package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import information.User;
import JDBCconnent.*;
import information.*;


public class UserDAOJDBC implements UserDAO {

	@Override
	public boolean login(String name, String password,Integer flag) throws Exception {
        String sql="select * from users";
        try(Connection con= JDBCUtil.getConnection();
        PreparedStatement pst=con.prepareStatement(sql);
        	){
        	ResultSet rs = pst.executeQuery(sql);
            while (rs.next()) {
                String username = rs.getString("username");
                String myPasssword = rs.getString("password");
                if(flag==1) {
                if (name.equals(username)&&password.equals(myPasssword)){
                    return true;
                }}
                if(flag==0) {
                	if(name.equals(username)) {
                		return true;
                	}
                }
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return false;
	}
	@Override
	public boolean loginName(String name) throws Exception {
		return login(name," ",0);
	}

	@Override
	public boolean loginPassword(String name, String password) throws Exception {

		return login(name,password,1);
	}
	@Override
	public boolean register(User user) throws Exception {
		// TODO 自动生成的方法存根
		String sql="insert into users(username,password,telephone,money) values(?,?,?,?) ";
        try (Connection con= JDBCUtil.getConnection();
             PreparedStatement pst=con.prepareStatement(sql);
        ) {
                pst.setString(1,user.getUserName());
                pst.setString(2,user.getPassword());
                pst.setString(3,user.getTelePhone());
                pst.setDouble(4,user.getMoney());
                pst.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return true;
		
	}

	@Override
	public boolean changePassword(String name, String newPassword) throws Exception {
		String sql="update users set password=? where username=?";
		String sql2="select * from users";
		try (Connection con= JDBCUtil.getConnection();
	             PreparedStatement pst=con.prepareStatement(sql);
	             PreparedStatement pst2=con.prepareStatement(sql2);
	        ) {
	            ResultSet rs=pst2.executeQuery(sql2);
	           
	            pst.setString(1,newPassword);
	            pst.setString(2,name);
	            pst.executeUpdate();
	        }catch (SQLException e){
	            e.printStackTrace();
	        }
	        return true;
	}

	@Override
	public boolean changeMoney(String name, double alterMoney) throws Exception {
		String sql="update users set money=? where username=?";
		String sql2="select * from users";
	        double thisMoney=0;
	        try (Connection con= JDBCUtil.getConnection();
	             PreparedStatement pst=con.prepareStatement(sql);
	             PreparedStatement pst2=con.prepareStatement(sql2);
	        ) {
	            ResultSet rs=pst2.executeQuery(sql2);
	            while (rs.next()){
	                if(name.equals(rs.getString("username"))){
	                    thisMoney=rs.getInt("money");
	                }

	            }
	            thisMoney=thisMoney+alterMoney;
	            pst.setDouble(1,thisMoney);
	            pst.setString(2,name);
	            pst.executeUpdate();
	        }catch (SQLException e){
	            e.printStackTrace();
	        }
	        return true;
	    }
	@Override
	public boolean passwordLimit(String password) throws Exception {
		int count = 0;
        if (password.length() - password.replaceAll("[A-Z]", "").length() > 0)
        {
            count++;
        }
        if (password.length() - password.replaceAll("[a-z]", "").length() > 0)
        {
            count++;
        }
        if (password.length() - password.replaceAll("[0-9]", "").length() > 0)
        {
            count++;
        }
        if (password.replaceAll("[0-9,A-Z,a-z]", "").length() > 0)
        {
            count++;
        }
		if(password.length()>=8&&count>=2&&password.length()<=16) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public User getMessage(String name) {
		String sql="select * from users";
        try(Connection con= JDBCUtil.getConnection();
        PreparedStatement pst=con.prepareStatement(sql);
        	){
        	ResultSet rs = pst.executeQuery(sql);
            while (rs.next()) {
                String username = rs.getString("username");
                
                
                if (name.equals(username)){
                    User user=new User(rs.getInt("id"),username, rs.getDouble("money"), rs.getString("telephone"));
                    return user;
                }
                
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return null;
	}


	
}
